/**
 * Copyright &copy; 2016-2022 liuhangjun All rights reserved.
 */
package com.channelsharing.hongqu.portal.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.channelsharing.cloud.aliyun.ocr.IdCardRecognizeUtil;
import com.channelsharing.common.exception.SystemInnerBusinessException;
import com.channelsharing.common.service.CrudServiceImpl;
import com.channelsharing.hongqu.portal.api.dao.UserCertificateDao;
import com.channelsharing.hongqu.portal.api.entity.UserCertificate;
import com.channelsharing.hongqu.portal.api.enums.Side;
import com.channelsharing.hongqu.portal.api.enums.UserSex;
import com.channelsharing.hongqu.portal.api.service.UserCertificateService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 用户认证信息Service
 *
 * @author liuhangjun
 * @version 2018-07-12
 */
@Service
public class UserCertificateServiceImpl extends CrudServiceImpl<UserCertificateDao, UserCertificate>
        implements UserCertificateService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public UserCertificate findOne(Long id) {
        UserCertificate entity = new UserCertificate();
        entity.setId(id);

        return super.findOne(entity);
    }

    @Override
    public void add(UserCertificate entity) {
        this.recognizeIdCard(entity);
        dao.insert(entity);
    }

    @Override
    public void modify(UserCertificate entity) {
        this.recognizeIdCard(entity);
        dao.update(entity);
    }

    private void recognizeIdCard(UserCertificate entity) {
        String realName, idCard, address, sex, birth, nationality, startDate, endDate, issue;

        try {
            JSONObject face = IdCardRecognizeUtil.recognize(entity.getCardFront(), Side.face.name());
            realName = face.getString("name");
            idCard = face.getString("card_no");
            address = face.getString("address");
            sex = face.getString("sex");
            birth = face.getString("date_of_birth");
            nationality = face.getString("nation");
        } catch (IOException e) {
            throw new SystemInnerBusinessException("身份证正面识别失败，请重新拍摄");
        }

        try {
            JSONObject back = IdCardRecognizeUtil.recognize(entity.getCardBack(), Side.back.name());
            startDate = back.getString("start_date");
            endDate = back.getString("end_date");
            issue = back.getString("issue");
        } catch (IOException e) {
            throw new SystemInnerBusinessException("身份证背面识别失败，请重新拍摄");
        }

        entity.setAddress(address);
        entity.setSex("男".equals(sex) ? UserSex.male.getCode() : UserSex.female.getCode());
        entity.setBirth(birth);
        entity.setNationality(nationality);
        entity.setStartDate(startDate);
        entity.setEndDate(endDate);
        entity.setIssue(issue);

        if (StringUtils.equals(entity.getRealName(), realName) && StringUtils.equals(entity.getIdCard(), idCard)) {
            entity.setApproveStatus(1);
        } else {
            entity.setApproveStatus(2);
        }
    }
}
